{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.6.12 64-bit ('tensorflow_cpu': conda)",
   "metadata": {
    "interpreter": {
     "hash": "36b3d205f4aeb453fdfdf0376bc3a1e6edb103cfd05ef6c54af0e932d6d60c21"
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import linear_model\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "def computeLine(y):\n",
    "    std = np.std(y, ddof=1)\n",
    "    x = []\n",
    "    for index, _ in enumerate(y):\n",
    "        x.append([index])\n",
    "    reg = linear_model.LinearRegression()\n",
    "    reg.fit(x, y)\n",
    "    k = float(np.squeeze(reg.coef_))\n",
    "    return std, k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "-2000.0 4472.13595499958\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import mean_squared_error as MSE\n",
    "y = [230000, 220000, 230000, 220000]\n",
    "x = []\n",
    "for index, _ in enumerate(y):\n",
    "        x.append([index])\n",
    "reg = linear_model.LinearRegression()\n",
    "reg.fit(x, y)\n",
    "k = float(np.squeeze(reg.coef_))\n",
    "y_pred = reg.predict(x)\n",
    "mse = MSE(y_pred, y)\n",
    "mse = np.sqrt(mse)\n",
    "print(k, mse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "0.1280765872286502 0.9794954847548272\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import mean_squared_error as MSE\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "y = [230000, 250000, 223453, 243333]\n",
    "x = []\n",
    "y = np.array(y).reshape(-1, 1)\n",
    "for index, _ in enumerate(y):\n",
    "        x.append([index])\n",
    "scaler = StandardScaler()\n",
    "scaler.fit(y)\n",
    "y = scaler.transform(y)\n",
    "\n",
    "reg = linear_model.LinearRegression()\n",
    "reg.fit(x, y)\n",
    "k = float(np.squeeze(reg.coef_))\n",
    "y_pred = reg.predict(x)\n",
    "mse = MSE(y_pred, y)\n",
    "#mse = np.sqrt(mse)\n",
    "print(k, mse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def computeLine(y):\n",
    "    x = []\n",
    "    y = np.array(y).reshape(-1, 1)\n",
    "    for index, _ in enumerate(y):\n",
    "            x.append([index])\n",
    "    scaler = StandardScaler()\n",
    "    scaler.fit(y)\n",
    "    y = scaler.transform(y)\n",
    "\n",
    "    reg = linear_model.LinearRegression()\n",
    "    reg.fit(x, y)\n",
    "    k = float(np.squeeze(reg.coef_))\n",
    "    y_pred = reg.predict(x)\n",
    "    mse = MSE(y_pred, y)\n",
    "    mse = np.sqrt(mse)\n",
    "    return k, mse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def computeLine(y):\n",
    "    x = []\n",
    "    for index, _ in enumerate(y):\n",
    "            x.append([index])\n",
    "    reg = linear_model.LinearRegression()\n",
    "    reg.fit(x, y)\n",
    "    k = float(np.squeeze(reg.coef_))\n",
    "    y_pred = reg.predict(x)\n",
    "    mse = MSE(y_pred, y)\n",
    "    mse = np.sqrt(mse)\n",
    "    return k, mse"
   ]
  }
 ]
}